#include "stdafx.h"
#include "DataManagerImp.h"


TDATA_API IDataManager* g_StaticDataManager = NULL;


DataManagerImp::DataManagerImp()
{

}

DataManagerImp::~DataManagerImp()
{
	for (DataMap::iterator it = m_theDataM.begin();
		it!=m_theDataM.end();
		it++)
	{
		delete it->second;
	}
}


bool
DataManagerImp::set(IData* pData)
{
	if (pData)
	{
		DataMap::iterator it = m_theDataM.find(pData->getID());
		if (it!=m_theDataM.end())
		{
			IData* pDataO = it->second;
			if (pDataO!=NULL && pDataO!=pData)
			{
				delete pDataO;
			}

			it->second = pData;
		}
		else
		{
			m_theDataM.insert(make_pair(pData->getID(), pData));
		}
		return true;
	}

	return false;
}

IData*
DataManagerImp::get(const int& nID, const int& nGrpID ) const
{
	DataMap::const_iterator it = m_theDataM.find(nID);
	if (it!=m_theDataM.end())
	{
		return it->second;
	}
	else
	{
		return NULL;
	}
}


int
DataManagerImp::size()const
{
	return (int)m_theDataM.size();
}

bool 
DataManagerImp::forEach(IDataVisitor& v)
{
	for (DataMap::iterator it = m_theDataM.begin();
		it!=m_theDataM.end();
		it++)
	{
		if (!v.visit(*it->second))
		{
			return false;
		}
	}

	return true;
}





DataManagerImpG::DataManagerImpG()
{

}

DataManagerImpG::~DataManagerImpG()
{
	for (DataMap::iterator it = m_theDataM.begin();
		it!=m_theDataM.end();
		it++)
	{
		delete it->second;
	}
}


bool
DataManagerImpG::set(IData* pData)
{
	if (pData)
	{
		__int64 key64 = (__int64(pData->getGroupID())<<32) | pData->getID();

		DataMap::iterator it = m_theDataM.find(key64);
		if (it!=m_theDataM.end())
		{
			IData* pDataO = it->second;
			if (pDataO!=NULL && pDataO!=pData)
			{
				delete pDataO;
			}

			it->second = pData;
		}
		else
		{
			m_theDataM.insert(make_pair(key64, pData));
		}
		return true;
	}

	return false;
}

IData*
DataManagerImpG::get(const int& nID, const int& nGrpID ) const
{
	__int64 key64 = (__int64(nGrpID)<<32) | nID;

	DataMap::const_iterator it = m_theDataM.find(key64);
	if (it!=m_theDataM.end())
	{
		return it->second;
	}
	else
	{
		return NULL;
	}
}


int
DataManagerImpG::size()const
{
	return (int)m_theDataM.size();
}

bool 
DataManagerImpG::forEach(IDataVisitor& v)
{
	for (DataMap::iterator it = m_theDataM.begin();
		it!=m_theDataM.end();
		it++)
	{
		if (!v.visit(*it->second))
		{
			return false;
		}
	}

	return true;
}
